package com.moniteach.server.servlet;

import java.io.IOException;
import java.sql.Date;

import javax.jdo.PersistenceManager;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.moniteach.server.db.bo.Task;
import com.moniteach.server.db.connector.PMF;
import com.moniteach.utils.Resource;

public class TaskServlet extends CommonServlet {
	private String action;
	private String id;
	private String type;
	private String title;
	private String text;
	private String dateStart;
	private String dateEnd;

	@Override
	protected void prepare(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("text/plain");
		resp.setCharacterEncoding("UTF-8");

		action = req.getParameter(Resource.Const.action);
		if ("".equals(action) || action == null) {
			resp.getWriter().print(Resource.Const.no + "ACTION");
			return;
		}

		id = req.getParameter(Resource.Const.id);
		type = req.getParameter(Resource.Const.type);
		title = req.getParameter(Resource.Const.title);
		text = req.getParameter(Resource.Const.text);
		dateStart = req.getParameter(Resource.Const.dateStart);
		dateEnd = req.getParameter(Resource.Const.dateEnd);

		String response = "";
		if (Resource.Action.CREATE.equals(action)) {
			response = create();
		} else if (Resource.Action.GET_BY_ID.equals(action)) {
			response = getById();
		} else if (Resource.Action.MODIFY.equals(action)) {
			response = modify();
		}
		resp.getWriter().print(response);

	}

	@Override
	protected String create() {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Date dateStartDate = Date.valueOf(dateStart);
			Date dateEndDate = Date.valueOf(dateEnd);
			Task oneTask = new Task(type, title, text, dateStartDate,
					dateEndDate);
			pm.makePersistent(oneTask);
			return String.valueOf(oneTask.getId());
		} catch (Exception e) {
			return Resource.Const.no;
		} finally {
			pm.close();
		}

	}

	@Override
	protected String get() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected String getById() {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Long idLong = Long.valueOf(id);
			Task entityTask = pm.getObjectById(Task.class, idLong);
			return entityTask.toJSONObject().toString();
		} catch (Exception e) {
			return Resource.Const.no;
		} finally {
			pm.close();
		}
	}

	@Override
	protected String getAll() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected String modify() {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Long idLong = Long.valueOf(id);
			Task entityTask = pm.getObjectById(Task.class, idLong);

			if (type != null) {
				entityTask.setType(type);
			}
			if (title != null) {
				entityTask.setTitle(title);
			}
			if (text != null) {
				entityTask.setText(text);
			}
			pm.makePersistent(entityTask);
			return Resource.Const.ok;
		} catch (Exception e) {
			return Resource.Const.no;
		} finally {
			pm.close();
		}
	}
}
